package com.ruoyi.common.utils;

import cn.hutool.json.JSONUtil;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

import java.util.HashMap;

public class AliSmsUtils {
	private static String SIGN_NAME = "帅先生";

	//产品名称:云通信短信API产品,开发者无需替换
	static final String product = "Dysmsapi";
	//产品域名,开发者无需替换
	static final String domain = "dysmsapi.aliyuncs.com";

	// TODO 此处需要替换成开发者自己的AK(在阿里云访问控制台寻找)
	static final String accessKeyId = "LTAIjq2s5C390tpR";
	static final String accessKeySecret = "8JpniDvA9dGITxz0RweyuJAabhyGTz";

	public static void main(String[] args) {
//		sendBuyWineMessage("18593979782","张",20);
	}


	public static void bookingSuccessMessage(String mobile, String name, String bookingInfo) {
		try {
			//可自助调整超时时间
			System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
			System.setProperty("sun.net.client.defaultReadTimeout", "10000");

			//初始化acsClient,暂不支持region化
			IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
			DefaultProfile.addEndpoint("cn-hangzhou", product, domain);
			IAcsClient acsClient = new DefaultAcsClient(profile);
			//组装请求对象-具体描述见控制台-文档部分内容
			SendSmsRequest request = new SendSmsRequest();
			//必填:待发送手机号
			request.setPhoneNumbers(mobile);
			//必填:短信签名-可在短信控制台中找到
			request.setSignName(SIGN_NAME);
			//必填:短信模板-可在短信控制台中找到
			request.setTemplateCode("SMS_246670210");
			request.setTemplateParam("{\"name\":\""+name+"\",\"bookingInfo\":\""+bookingInfo+"\"}");
			//hint 此处可能会抛出异常，注意catch
			SendSmsResponse response = acsClient.getAcsResponse(request);

			System.out.println("短信接口返回的数据----------------");
			System.out.println("Code=" + response.getCode());
			System.out.println("Message=" + response.getMessage());
			System.out.println("RequestId=" + response.getRequestId());
			System.out.println("BizId=" + response.getBizId());

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void platformPaySuccessMessage(String mobile, String name, Integer useTicket, Integer leftTicket) {
		try {
			//可自助调整超时时间
			System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
			System.setProperty("sun.net.client.defaultReadTimeout", "10000");

			//初始化acsClient,暂不支持region化
			IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
			DefaultProfile.addEndpoint("cn-hangzhou", product, domain);
			IAcsClient acsClient = new DefaultAcsClient(profile);
			//组装请求对象-具体描述见控制台-文档部分内容
			SendSmsRequest request = new SendSmsRequest();
			//必填:待发送手机号
			request.setPhoneNumbers(mobile);
			//必填:短信签名-可在短信控制台中找到
			request.setSignName(SIGN_NAME);
			//必填:短信模板-可在短信控制台中找到
			request.setTemplateCode("SMS_261325117");
			HashMap<String,Object> paramMap = new HashMap<>(3);
			paramMap.put("name",name);
			paramMap.put("useTicket",useTicket);
			paramMap.put("leftTicket",leftTicket);
			request.setTemplateParam(JSONUtil.toJsonStr(paramMap));
			//hint 此处可能会抛出异常，注意catch
			SendSmsResponse response = acsClient.getAcsResponse(request);

			System.out.println("短信接口返回的数据----------------");
			System.out.println("Code=" + response.getCode());
			System.out.println("Message=" + response.getMessage());
			System.out.println("RequestId=" + response.getRequestId());
			System.out.println("BizId=" + response.getBizId());

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void noticeSendMessage(String mobile, String name, String goodsInfo,
										 Integer buyNum, String address, String phone) {
		try {
			//可自助调整超时时间
			System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
			System.setProperty("sun.net.client.defaultReadTimeout", "10000");

			//初始化acsClient,暂不支持region化
			IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
			DefaultProfile.addEndpoint("cn-hangzhou", product, domain);
			IAcsClient acsClient = new DefaultAcsClient(profile);
			//组装请求对象-具体描述见控制台-文档部分内容
			SendSmsRequest request = new SendSmsRequest();
			//必填:待发送手机号
			request.setPhoneNumbers(mobile);
			//必填:短信签名-可在短信控制台中找到
			request.setSignName(SIGN_NAME);
			//必填:短信模板-可在短信控制台中找到
			request.setTemplateCode("SMS_243425052");
			HashMap<String,Object> paramMap = new HashMap<>(3);
			paramMap.put("name",name);
			paramMap.put("goodsInfo",goodsInfo);
			paramMap.put("buyNum",buyNum);
			paramMap.put("address",address);
			paramMap.put("phone",phone);
			request.setTemplateParam(JSONUtil.toJsonStr(paramMap));
			//hint 此处可能会抛出异常，注意catch
			SendSmsResponse response = acsClient.getAcsResponse(request);

			System.out.println("短信接口返回的数据----------------");
			System.out.println("Code=" + response.getCode());
			System.out.println("Message=" + response.getMessage());
			System.out.println("RequestId=" + response.getRequestId());
			System.out.println("BizId=" + response.getBizId());

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
